#include "piece_guard.hpp"

bool Piece_Guard::move(const Coordinate &coor_end) {
    Coordinate det = coor_end - coor;
    if (det * det == 2) {
        if (color == RED) {
            if (abs(8 - coor_end.x) <= 1 && abs(4 - coor_end.y) <= 1) {
                if (pieces[gameMap[coor_end]]->isLife()) {
                    if (pieces[gameMap[coor_end]]->getColor() != color) {
                        pieces[gameMap[coor_end]]->setLife(false);
                        updateCoor(gameMap, coor_end);
                        return true;
                    }
                    return false;
                }
                updateCoor(gameMap, coor_end);
                return true;
            }
        }
        if (abs(1 - coor_end.x) <= 1 && abs(4 - coor_end.y) <= 1) {
            if (pieces[gameMap[coor_end]]->isLife()) {
                if (pieces[gameMap[coor_end]]->getColor() != color) {
                    pieces[gameMap[coor_end]]->setLife(false);
                    updateCoor(gameMap, coor_end);
                    return true;
                }
                return false;
            }
            updateCoor(gameMap, coor_end);
            return true;
        }
    }
    return false;
}


void Piece_Guard::init() {
    switch (pt) {
        case PieceType::RED_GUARD1:
            coor.x = 9;
            coor.y = 3;
            color = RED;
            break;
        case PieceType::RED_GUARD2:
            coor.x = 9;
            coor.y = 5;
            color = RED;
            break;
        case PieceType::BLACK_GUARD1:
            coor.x = 0;
            coor.y = 3;
            color = BLACK;
            break;
        case PieceType::BLACK_GUARD2:
            coor.x = 0;
            coor.y = 5;
            color = BLACK;
            break;
    }
    life = true;
}

Piece_Guard::~Piece_Guard() {}
